package com.changgou.business.listener;

import okhttp3.*;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

@Component
public class AdListener {


    @RabbitListener(queues = "ad_update_queue")
    public void receiveMessage(Message message) {
        String position = new String(message.getBody());
        System.out.println("更新广告:" + position);

//        RestTemplate restTemplate = new RestTemplate();
//        String url="http://192.168.200.128/ad_update?position="+position;
//        String result = restTemplate.getForObject(url, String.class);
//        System.out.println(result);

        OkHttpClient okHttpClient = new OkHttpClient();
        String url = "http://192.168.200.128/ad_update?position=" + position;

        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("请求结果:"+response.message());
            }
        });
    }
}
